﻿	<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%>
	<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
	<%@ page import="com.cxx.purchasecharge.component.utils.ComponentConstants" %>
	<%@ page import="com.cxx.purchasecharge.component.bean.TimeFrame" %>
	
	<script type="text/javascript">
		$(function() 
		{
			//$('#toolbar-accounting #advanceSearchSpan #accountingMode').combobox('setValue', '');
			//$('#dg-accounting').datagrid('options').onBeforeLoad = onBeforeLoadAccounting;
			//$('#dg-accounting').datagrid('reload');
		});
		var onClickExpenseTypeCombox2 = function() 
		{
			//$(this).combobox('reload', '<c:url value='/accountingType/getAllType.html' />');
			var mode = $('#toolbar-accounting #advanceSearchSpan #accountingMode').combo('getValue');
			if(mode == undefined || mode == '') 
			{
				$.messager.alert('警告','请先选择主类型!','warning');
				return;
			}
			$(this).combobox('reload', '<c:url value='/accountingType/getTypeByMode.html' />?accountingMode=' + mode);
		}
		var onClickExpenseTypeCombox = function() 
		{
			//$(this).combobox('reload', '<c:url value='/accountingType/getAllType.html' />');
			var tab = $('#tt_accounting').tabs('getSelected');
			var index = $('#tt_accounting').tabs('getTabIndex',tab);
			if(0 == index) 
			{
				$(this).combobox('reload', '<c:url value='/accountingType/getTypeByMode.html' />?accountingMode=OUT_LAY');
			}
			else 
			{
				$(this).combobox('reload', '<c:url value='/accountingType/getTypeByMode.html' />?accountingMode=IN_COME');
			}
		}
		
		var onSelectStartDate = function (date) 
		{
			$('#toolbar-accounting #advanceSearchSpan #timeFrame').combobox('setValue', 'CUSTOMIZE');
		}
		var onSelectTimeFrame = function(record) 
		{
			var startDate = generateStartDate(record.value);
			var endDate = new Date().format("yyyy-MM-dd");
			$('#toolbar-accounting #advanceSearchSpan #startDate').datebox('setValue', startDate);
			$('#toolbar-accounting #advanceSearchSpan #endDate').datebox('setValue', endDate);
		}
		
		var initial = false;
		var onBeforeLoadAccounting = function (param) 
		{
			//console.log(param);
			if(initial == false) 
			{
				$('#toolbar-accounting #advanceSearchSpan #accountingMode').combobox('setValue', '');
			}
			initial = true;
			var startDate = $('#toolbar-accounting #advanceSearchSpan #startDate').combo('getValue');
			var endDate = $('#toolbar-accounting #advanceSearchSpan #endDate').combo('getValue');
			var mode = $('#toolbar-accounting #advanceSearchSpan #accountingMode').combo('getValue');
			var typeId = $('#toolbar-accounting #advanceSearchSpan #typeId').combo('getValue');
			if(typeId == undefined) 
			{
				typeId = '';
			}
			if(startDate == '' && endDate == '' && mode == '' && typeId == '') 
			{
				var timeFrame = $('#toolbar-accounting #advanceSearchSpan #timeFrame').combo('getValue');
				startDate = generateStartDate(timeFrame);
				endDate = new Date().format("yyyy-MM-dd");
				$('#toolbar-accounting #advanceSearchSpan #startDate').datebox('setValue', startDate);
				$('#toolbar-accounting #advanceSearchSpan #endDate').datebox('setValue', endDate);
				
				startDate = $('#toolbar-accounting #advanceSearchSpan #startDate').combo('getValue');
				endDate = $('#toolbar-accounting #advanceSearchSpan #endDate').combo('getValue');
			}
			
			if(param.page == undefined || param.page == 0) 
			{
				param.page = 1;
			}
			$('#dg-accounting').datagrid('options').url = "<c:url value='/accounting/getModelBySearchForm.html' />?startDate="+startDate+"&endDate="+endDate+"&accountingMode=" + mode + "&accountingTypeId="+typeId+"&page="+param.page;
			
			return true;
		}
		
		var onSearchAccountingByAdvance = function () 
		{
			var startDate = $('#toolbar-accounting #advanceSearchSpan #startDate').combo('getValue');
			var endDate = $('#toolbar-accounting #advanceSearchSpan #endDate').combo('getValue');
			var mode = $('#toolbar-accounting #advanceSearchSpan #accountingMode').combo('getValue');
			var typeId = $('#toolbar-accounting #advanceSearchSpan #typeId').combo('getValue');
			if((startDate != '' && endDate == '') || (startDate == '' && endDate != '')) 
			{
				$.messager.alert('警告','开始时间和结束时间必须同时填!','warning');
				return;
			}
			if(startDate == '' && endDate == '' && mode == '' && typeId == '') 
			{
				$.messager.alert('警告','请填写查询条件!','warning');
				return;
			}
			
			$('#dg-accounting').datagrid('reload');
			//loadGridData ('#dg-accounting', '<c:url value='/accounting/getModelBySearchForm.html' />', {page:sortOrder.page, rows:sortOrder.rows, sort:sortOrder.sort, order:sortOrder.order, startDate:startDate, endDate:endDate, accountingMode:mode, accountingTypeId:typeId});
		}
		var newAccounting = function () 
		{
			newModel('#dlg-accounting', '<spring:message code="newType" />', '#fm-accounting-out', '<c:url value='/accounting/addModel.html' />', newAccountingCallback);
		}
		var newAccountingCallback = function () 
		{
			//var createDate = new Date().format("yyyy-MM-dd");
			//$('#dlg-accounting #fm-accounting-out #createDate').datebox('setValue', createDate);
			var createDate = new Date().format("yyyy-MM-dd hh:mm");
			$('#dlg-accounting #fm-accounting-out #createDate').datetimebox('setValue', createDate);
			$('#dlg-accounting #fm-accounting-in').form('clear');
			//$('#dlg-accounting #fm-accounting-in #createDate').datebox('setValue', createDate);
			$('#dlg-accounting #fm-accounting-in #createDate').datetimebox('setValue', createDate);
		}
		var saveAccounting = function () 
		{
			var tab = $('#tt_accounting').tabs('getSelected');
			var index = $('#tt_accounting').tabs('getTabIndex',tab);
			if(0 == index) 
			{
				saveModel('#dg-accounting', '#dlg-accounting', '#fm-accounting-out');
			}
			else 
			{
				saveModel('#dg-accounting', '#dlg-accounting', '#fm-accounting-in');
			}
		}
		
		var editAccounting = function()
		{
			$('#dlg-accounting #fm-accounting-in').form('clear');
			$('#dlg-accounting #fm-accounting-out').form('clear');
			var row = $('#dg-accounting').datagrid('getSelected');
			if (row) {
				$('#dlg-accounting').dialog('open').dialog('setTitle', '编辑记账');
				if(row.typeBean) 
				{
					if('IN_COME' == row.typeBean.accountingMode) 
					{
						$('#tt_accounting').tabs('enableTab',1);
						$('#tt_accounting').tabs('select',1);
						$('#tt_accounting').tabs('disableTab',0);
						$('#dlg-accounting #fm-accounting-in').form('load', row);
						//$('#dlg-accounting #fm-accounting-in #typeId').combobox('setValue', row.typeBean.id);
						fillSelectedAccountingType('<c:url value='/accountingType/getTypeByMode.html' />?accountingMode=IN_COME', '#dlg-accounting #fm-accounting-in #typeId', row.typeBean.id);
					}
					if('OUT_LAY' == row.typeBean.accountingMode) 
					{
						$('#tt_accounting').tabs('enableTab',0);
						$('#tt_accounting').tabs('select',0);
						$('#tt_accounting').tabs('disableTab',1);
						$('#dlg-accounting #fm-accounting-out').form('load', row);
						//$('#dlg-accounting #fm-accounting-out #typeId').combobox('setValue', row.typeBean.id);
						fillSelectedAccountingType('<c:url value='/accountingType/getTypeByMode.html' />?accountingMode=OUT_LAY', '#dlg-accounting #fm-accounting-out #typeId', row.typeBean.id);
					}
					setSaveOrEditUrl('<c:url value='/accounting/updateModel.html' />');
				}
			}
		}
		var fillSelectedAccountingType = function(requestUrl, comboboxId, selectedId) 
		{
			$.post(requestUrl, 
				function(result) 
				{
					//console.log(result);
					$(comboboxId).combobox('loadData', result);
					$(comboboxId).combobox('setValue', selectedId);
				}, 
				'json');
		}
		
		function formatter_accountingMode2(value,row)
		{
			if(row.typeBean) 
			{
				if('IN_COME' == row.typeBean.accountingMode) 
				{
					return '收入';
				}
				if('OUT_LAY' == row.typeBean.accountingMode) 
				{
					return '支出';
				}
			}
			return '<span style="font-weight:bold">' + value + '</span>';
		}
		function formatter_accountingMoney(value,row)
		{
			if(row.typeBean) 
			{
				if('IN_COME' == row.typeBean.accountingMode) 
				{
					return '<span style="color:green">+' + value + '</span>';
				}
				if('OUT_LAY' == row.typeBean.accountingMode) 
				{
					return '<span style="color:red">-' + value + '</span>';
				}
			}
			return '<span style="font-weight:bold">' + value + '</span>';
		}
		function formatter_accountingType(value,row)
		{
			if(row.typeBean) 
			{
				return row.typeBean.name;
			}
			return '';
		}
		function onChangeAccountingMode(newValue, oldValue) 
		{
			$('#toolbar-accounting #advanceSearchSpan #typeId').combobox('setValue', '');
		}
		
		var onClickAccountingRow = function (rowIndex, rowData) 
		{
			$('#toolbar-accounting #btn-editAccounting').linkbutton('enable');
			//$('#toolbar-accounting #btn-deleteAccounting').linkbutton('enable');
		}
		var onLoadAccountingSuccess = function (data) 
		{
			$('#toolbar-accounting #btn-editAccounting').linkbutton('disable');
			$('#toolbar-accounting #btn-deleteAccounting').linkbutton('disable');
		}
		var onCheckAllAccounting = function(rows) 
		{
			$('#toolbar-accounting #btn-deleteAccounting').linkbutton('enable');
		}
		var onUnCheckAccounting = function(rowIndex,rowData) 
		{
			var rows = $('#dg-accounting').datagrid('getChecked');
			if (rows.length == 0) {
				$('#toolbar-accounting #btn-deleteAccounting').linkbutton('disable');
			}
		}
	</script>
	
	<table id="dg-accounting" title="<spring:message code="navigation.expenseRecordManagement" />" class="easyui-datagrid"
		style="" url="" showFooter="true" fit="true" 
		toolbar="#toolbar-accounting" rownumbers="true" singleSelect="true" checkOnSelect="true" selectOnCheck="false"
		data-options="fitColumns:true, pagination:true, sortName:'<%=ComponentConstants.EXPENSE_CREATE_DATE %>', sortOrder:'desc', 
			onClickRow:onClickAccountingRow, onLoadSuccess:onLoadAccountingSuccess, onCheckAll:onCheckAllAccounting, onUncheckAll:onUnCheckAccounting, onCheck:onCheckAllAccounting, onUncheck:onUnCheckAccounting,onBeforeLoad:onBeforeLoadAccounting">
		<thead>
			<tr>
				<th data-options="field:'ck',checkbox:true"></th>
				<th field="id" width="" data-options="hidden:true"><spring:message code="id" /></th>
				<th field="mode" width="50" data-options="sortable:true, formatter:formatter_accountingMode2">支/收</th>
				<th field="<%=ComponentConstants.EXPENSE_TYPE %>" width="50" data-options="sortable:true, formatter:formatter_accountingType">类型</th>
				<th field="money" width="50" data-options="sortable:true, formatter:formatter_accountingMoney">金额</th>
				<th field="<%=ComponentConstants.EXPENSE_CREATE_DATE %>" width="50" data-options="sortable:true">创建时间</th>
				<th field="<%=ComponentConstants.EXPENSE_UPDATE_DATE %>" width="50" data-options="sortable:true">更新时间</th>
				<th field="userCreated" width="50" data-options="sortable:true">创建人</th>
				<th field="comment" width="100" data-options="sortable:true">备注</th>
			</tr>
		</thead>
	</table>
	
	<div id="toolbar-accounting" style="padding:5px;height:auto">
		<div>
			<a href="javascript:void(0)" class="easyui-linkbutton" 
				iconCls="icon-add" plain="true" onclick="newAccounting()" title="新增记账">新增记账</a> 
			<a id="btn-editAccounting" href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-edit" plain="true" onclick="editAccounting()" title="<spring:message code="selectOneRow" />编辑记账">编辑记账</a> 
			<a id="btn-deleteAccounting" href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-remove" plain="true" onclick="destroyMultipleModel('#dg-accounting', '<spring:message code="expense.expense" />', '<c:url value='/accounting/deleteModels.html' />')" title="<spring:message code="checkOneOrMultiple" />删除记账">删除记账</a>
		</div>	
		<div>
			<span id="advanceSearchSpan" style="width: 100%; display: inline-block; margin-left: 8px;">
				<form id="outOrderSearchForm" action="">
	            	时间: <input id="startDate" name="startDate" class="easyui-datebox" style="width:100px" 
					editable="false" title="开始时间" data-options="onSelect: onSelectStartDate">
	            	- <input id="endDate" name="endDate" class="easyui-datebox" style="width:100px" 
					editable="false" title="结束时间" data-options="onSelect: onSelectStartDate">&nbsp;
					<select id="timeFrame" name="timeFrame" class="easyui-combobox" style="width:80px" panelHeight="auto" editable="false" data-options="onSelect: onSelectTimeFrame">
						<option value="CUSTOMIZE"><%=TimeFrame.CUSTOMIZE.getText () %></option>
						<option value="TODAY"><%=TimeFrame.TODAY.getText () %></option>
						<option value="RECENT_THREE_DAYS"><%=TimeFrame.RECENT_THREE_DAYS.getText () %></option>
						<option value="RECENT_SEVEN_DAYS"><%=TimeFrame.RECENT_SEVEN_DAYS.getText () %></option>
						<option value="RECENT_FIFTEEN_DAYS"><%=TimeFrame.RECENT_FIFTEEN_DAYS.getText () %></option>
						<option value="RECENT_THIRTY_DAYS"><%=TimeFrame.RECENT_THIRTY_DAYS.getText () %></option>
						<option value="CURRENT_MONTH" selected="selected"><%=TimeFrame.CURRENT_MONTH.getText () %></option>
					</select>
					
					&nbsp;
	            	主类型:
					<select id="accountingMode" name="accountingMode" class="easyui-combobox" panelHeight="auto" style="width:80px" data-options="onChange:onChangeAccountingMode">
						<option value="OUT_LAY">支出</option>
						<option value="IN_COME">收入</option>
					</select>
	            	&nbsp;
	            	子类型: 
	           		<input id="typeId" name="typeId" class="easyui-combobox" style="width:100px" data-options="valueField:'id',
												textField:'name',
												url:'',
												mode:'local',
												filter:comboboxFilter,
												onShowPanel:onClickExpenseTypeCombox2" />
					<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" plain="true" title="查询出库单" onclick="onSearchAccountingByAdvance()">查询</a>
				</form>
	        </span>
        </div>
	</div>

	<div id="dlg-accounting" class="easyui-dialog"
		style="width: 600px; height: 300px; padding: 5px" closed="true"
		buttons="#dlg-buttons-accounting" data-options="modal:true">
		
		<div id="tt_accounting" class="easyui-tabs" style="width:550px; height:200px">
	        <div id="tab_out_lay" title="支出" style="padding:10px">
	            <form id="fm-accounting-out" class="fm" method="post" novalidate>
					<div class="fitem divHidden">
						<input id="expenseId" name="id">
					</div>
					<div class="fitem divHidden">
						<input id="userCreated" name="userCreated">
					</div>
					<div class="fitem">
						<label>支出类型:</label> 
						<input id="typeId" name="typeBean.id" class="easyui-combobox" editable="false" data-options="valueField:'id',
												textField:'name',
												url:'',
												required: true,
												mode:'local',
												filter:comboboxFilter,
												onShowPanel:onClickExpenseTypeCombox" />
					</div>
					<div class="fitem">
						<label>金额:</label> 
						<input id="money" name="money" class="easyui-numberbox" data-options="precision:2" required="true" 
							onkeyup="if(event.keyCode == 13) {$('#save-accounting').click()}" />
					</div>
					<div class="fitem">
						<label>日期:</label> 
						<input id="createDate" name="createDate" class="easyui-datetimebox" editable="false" required="true" 
							data-options="showSeconds:false" />
					</div>
					<div class="fitem">
						<label>备注:</label> 
						<input id="comment" name="comment" class="easyui-validatebox" onkeyup="" />
					</div>
				</form>
	        </div>
	        <div id="tab_in_come" title="收入" style="padding:10px">
	            <form id="fm-accounting-in" class="fm" method="post" novalidate>
					<div class="fitem divHidden">
						<input id="expenseId" name="id">
					</div>
					<div class="fitem divHidden">
						<input id="userCreated" name="userCreated">
					</div>
					<div class="fitem">
						<label>支出类型:</label> 
						<input id="typeId" name="typeBean.id" class="easyui-combobox" editable="false" data-options="valueField:'id',
												textField:'name',
												url:'',
												required: true,
												mode:'local',
												filter:comboboxFilter,
												onShowPanel:onClickExpenseTypeCombox" />
					</div>
					<div class="fitem">
						<label>金额:</label> 
						<input id="money" name="money" class="easyui-numberbox" data-options="precision:2" required="true" 
							onkeyup="if(event.keyCode == 13) {$('#save-accounting').click()}" />
					</div>
					<div class="fitem">
						<label>日期:</label> 
						<input id="createDate" name="createDate" class="easyui-datetimebox" editable="false" required="true" 
							data-options="showSeconds:false" />
					</div>
					<div class="fitem">
						<label>备注:</label> 
						<input id="comment" name="comment" class="easyui-validatebox" onkeyup="" />
					</div>
				</form>
	        </div>
	    </div>
		
	</div>
	<div id="dlg-buttons-accounting">
		<a id="save-accounting" href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-ok" onclick="saveAccounting()"><spring:message code="save" /></a> 
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-cancel" onclick="javascript:$('#dlg-accounting').dialog('close')"><spring:message code="cancel" /></a>
	</div>
